﻿using System;
using System.IO;
using System.Collections.ObjectModel;
using System.Linq;
using System.Xml.Linq;
using System.Text;
using MarketDashboardModule.Models;
using System.Text.RegularExpressions;

namespace MarketDashboardModule.Helpers
{
    public class StockSymbolLoader
    {
        private Action<bool> _isdone;

        public StockSymbolLoader()
        {            
        }

        public void Load(Action<bool> action)
        {
            System.Net.WebClient client = new System.Net.WebClient();
            client.DownloadDataCompleted += new System.Net.DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
            client.DownloadDataAsync(new Uri("http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download"));
            _isdone = action;
        }

        void client_DownloadDataCompleted(object sender, System.Net.DownloadDataCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                throw new Exception(e.Error.Message.ToString());
            }

            byte[] data = e.Result;
            System.Text.Encoding enc = System.Text.Encoding.ASCII;
            string myString = enc.GetString(data);            
            MemoryStream ms = new MemoryStream(data);
            ProcessData(ms);
        }

        private void ProcessData(MemoryStream myString)
        {
            StreamReader sr = new StreamReader(myString);
            StringBuilder sbld = new StringBuilder();
            int counter = 0;
            string line ;
            Stock stock;
            while ((line = sr.ReadLine()) != null)
            {                
                if (counter > 0)
                {
                    //string temp = line.Replace("\"", "");
                    string[] contents = Regex.Split(line, "\",\"");
                    stock = new Stock() 
                    { 
                        Symbol = contents[0].Replace("\"", ""), 
                        Name = contents[1].Replace("\"", ""), 
                        LastSale = GetDoubleNumber(contents[2].Replace("\"", "")),
                        MarketCap = GetDoubleNumber(contents[3].Replace("\"", "")),
                        IPOyear = GetNumber(contents[5].Replace("\"", "")),
                        Sector = contents[6].Replace("\"", ""),
                        Industry = contents[7].Replace("\"", "")                                                
                    };
                    Global.Stocks.Add(stock); 
                }
                counter++;
            }
            _isdone(true);
        }

        private double GetDoubleNumber(string content)
        {
            double result;
            if (Double.TryParse(content, out result))
            {
                return result;
            }
            return result;
        }

        private int GetNumber(string content)
        {
            int result;
            if (int.TryParse(content, out result))
            {
                return result;
            }
            return result;
        }
    }
}